package com.ztt.service.impl;

import com.ztt.pojo.entity.zttProduct;
import com.ztt.mapper.ProductMapper;
import com.ztt.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper ;

    @Override
    public List<zttProduct> SelectAllProduct(){
        return productMapper.SelectAllProduct();
    };

    @Override
    public void InsertProduct(zttProduct product){
        productMapper.InsertProduct(product);
    };

    public zttProduct getProductById(zttProduct product) {
        return productMapper.getProductById(product);
    }

    public zttProduct getProductByName(zttProduct product) {
        return productMapper.getProductByName(product);
    }

    public void UpdateProduct(zttProduct product) {
        String productID = product.getProductID();
        if (!Objects.equals(productID, "")) {
            zttProduct product1 = productMapper.getProductById(product);
            String productName = product.getProductName();
            if (!Objects.equals(productName, "")) {
                product1.setProductName(productName);
            }
            float productPrice = product.getProductPrice();
            if (productPrice != product1.getProductPrice()) {
                product1.setProductPrice(productPrice);
            }
            String supplierID = product.getSupplierID();
            if (!Objects.equals(supplierID, "")) {
                product1.setSupplierID(supplierID);
            }
            String productDescription = product.getProductDescription();
            if (!Objects.equals(productDescription, "")) {
                product1.setProductDescription(productDescription);
            }
            String notes = product.getNotes();
            if (!Objects.equals(notes, "")) {
                product1.setNotes(notes);
            }
            productMapper.UpdateProduct(product1);
        }

    }


    public void DeleteProduct(zttProduct product) {
        productMapper.DeleteProduct(product);
    }


}
